{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e41d3456",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "> 1. while循环\n",
    "> 2. 购物车实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c02bd6f9",
   "metadata": {},
   "source": [
    "## 1.while循环\n",
    "* 回顾：for循环：for+range()可以指定循环的次数--关于网页URL队列的抓取实践\n",
    "* while循环 当某种条件成立时，停止循环\n",
    "* while True：无限循环/死循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f1bde7a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙 0\n",
      "你是风儿我是沙 1\n",
      "你是风儿我是沙 2\n",
      "你是风儿我是沙 3\n",
      "你是风儿我是沙 4\n",
      "你是风儿我是沙 5\n",
      "你是风儿我是沙 6\n",
      "你是风儿我是沙 7\n",
      "你是风儿我是沙 8\n",
      "你是风儿我是沙 9\n"
     ]
    }
   ],
   "source": [
    "count=0\n",
    "while True:\n",
    "    print(\"你是风儿我是沙\",count)\n",
    "    count=count+1\n",
    "    if count ==10:\n",
    "        break\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a4d55512",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：admin\n",
      "密码：123456\n",
      "欢迎admin\n"
     ]
    }
   ],
   "source": [
    "# 2. while Ture循环，可以让用户重复输入\n",
    "user01 = \"admin\"\n",
    "password01 = \"123456\"\n",
    "count = 0\n",
    "\n",
    "while True:\n",
    "    username = input(\"账号：\")\n",
    "    password = input(\"密码：\")\n",
    "    if user01 ==username and password01 ==password:\n",
    "        print(\"欢迎\"+user01)\n",
    "        break\n",
    "    else:\n",
    "        if count<2:\n",
    "            print(\"n密码不正确，你还有\"+str(2-count)+\"次机会\")\n",
    "        else:\n",
    "            print(\"请一小时再来\")\n",
    "            break\n",
    "        count = count+1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c844abf",
   "metadata": {},
   "source": [
    "## 2.购物车项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad225d81",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：8\n",
      "----------商品列表----------\n",
      "商品序号 (0)： 蜜雪柠檬水 6元\n",
      "商品序号 (1)： 蜜桃四季春 10元\n",
      "商品序号 (2)： 芒果奶盖 12元\n",
      "商品序号 (3)： 葡萄波波奶茶 8元\n",
      "请输入商品编号：0\n",
      "商品购买成功，正在制作中...您的余额还有2元\n",
      "----------商品列表----------\n",
      "商品序号 (0)： 蜜雪柠檬水 6元\n",
      "商品序号 (1)： 蜜桃四季春 10元\n",
      "商品序号 (2)： 芒果奶盖 12元\n",
      "商品序号 (3)： 葡萄波波奶茶 8元\n",
      "请输入商品编号：1\n",
      "您的余额不足，请更换支付方式~~~\n",
      "----------商品列表----------\n",
      "商品序号 (0)： 蜜雪柠檬水 6元\n",
      "商品序号 (1)： 蜜桃四季春 10元\n",
      "商品序号 (2)： 芒果奶盖 12元\n",
      "商品序号 (3)： 葡萄波波奶茶 8元\n"
     ]
    }
   ],
   "source": [
    "# 1. 运行程序后，让用户输入支付宝余额 ✔，然后打印我们的商品列表给用户。✔\n",
    "product_list = [\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄波波奶茶\",8]\n",
    "]\n",
    "shopping_car =[]\n",
    "AliPay = int(input(\"请输入余额：\"))\n",
    "while True:\n",
    "    print(\"----------商品列表----------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品序号\",\"(\"+str(i)+\")：\",product_list[i][0],str(product_list[i][1])+\"元\")  # 格式化输出\n",
    "    # 2. 让用户输入商品编号进行商品的购买。:直接用商品列表的 index 作为商品标号\n",
    "    user_choice = input(\"请输入商品编号：\")\n",
    "    # 情景1：用户正在购买，输入商品编号\n",
    "    if user_choice.isdigit():\n",
    "        # 情景一：余额大于商品价格\n",
    "        if AliPay >= product_list[int(user_choice)][1]:\n",
    "            AliPay = AliPay - product_list[int(user_choice)][1]\n",
    "            shopping_car.append(product_list[int(user_choice)])   \n",
    "            print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(AliPay))\n",
    "        # 情景二：余额小于商品价格\n",
    "        else:\n",
    "            print(\"您的余额不足，请更换支付方式~~~\")\n",
    "    # 情景2：用户输入退出，结束购买\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"您已购买的商品：\",shopping_car)  \n",
    "        print(\"您的余额还有%s,欢迎下次光临~~\"%(AliPay)) \n",
    "        break\n",
    "\n",
    "             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebee2c2e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2abc7eb0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
